package com.foot.travel;

import java.util.HashMap;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.jpush.android.api.JPushInterface;

import com.koalac.ironman.weixin.FootTravelWechat;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * @author: chenxiaohua
 * @date: 2013-6-10
 * @copyright: lifeq.cn
 */
public class LifeqApplication extends Application {
	private Object data;

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	@Override
	public void onCreate() {
		super.onCreate();
//		initJPush();
		Context context = getApplicationContext();
		data = new HashMap<String, Object>();
		initImageLoader(context);
		FootTravelWechat.INSTANCE.getWXAPI(getApplicationContext());
		initGalleryFinal();
	}

	private void initGalleryFinal() {
	    ThemeConfig theme = new ThemeConfig.Builder()
	    .setTitleBarBgColor(getResources().getColor(R.color.foot_nav))
	        .setTitleBarTextColor(Color.WHITE)
	        .setTitleBarIconColor(Color.WHITE)
	        .setFabNornalColor(getResources().getColor(R.color.foot_nav))
	        .setFabPressedColor(Color.BLUE)
	        .setCheckNornalColor(getResources().getColor(R.color.transparency))
	        .setCheckSelectedColor(getResources().getColor(R.color.foot_nav))
	        .build();
	//配置功能
	FunctionConfig functionConfig = new FunctionConfig.Builder()
	        .setEnableCamera(true)
	        .setEnableEdit(true)
	        .setEnableCrop(true)
	        .setEnableRotate(true)
	        .setCropSquare(true)
	        .setEnablePreview(true)
	        .build();

	//配置imageloader
	CoreConfig coreConfig = new CoreConfig.Builder(getApplicationContext(), new UILImageLoader(), theme)
	        .setDebug(BuildConfig.DEBUG)
	        .setFunctionConfig(functionConfig)
	        .build();
	GalleryFinal.init(coreConfig);
	}
	
	/**
	 * 初始化极光推送
	 */
	private void initJPush() {
		JPushInterface.setDebugMode(true);
		JPushInterface.init(this);
	}

	/**
	 * 初始化图片加载器
	 */
	private void initImageLoader(Context context) {
	    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
			context).threadPoolSize(3).memoryCache(new WeakMemoryCache())
			.memoryCacheExtraOptions(480, 800).diskCacheExtraOptions(480, 800, null)
			.threadPriority(Thread.NORM_PRIORITY - 2)
			.denyCacheImageMultipleSizesInMemory()
			.diskCacheFileNameGenerator(new Md5FileNameGenerator())
			.tasksProcessingOrder(QueueProcessingType.LIFO)
			.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
			.writeDebugLogs()// Remove for release app
			.build();
	ImageLoader.getInstance().init(config);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		try {
			Log.i("ArmisiFamilyApplication", "MMClear");
			ImageLoader.getInstance().clearMemoryCache();
		} catch (Exception ex) {
			Log.i("ArmisiFamilyApplication", "MMClear err:" + ex);
		}
	}

}
